package com.neusoft.ruidaozaixian.controller.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.neusoft.ruidaozaixian.R;
import com.neusoft.ruidaozaixian.controller.activity.LoginActivity;
import com.neusoft.ruidaozaixian.controller.activity.PostActivity;
import com.neusoft.ruidaozaixian.model.bean.PostInformation;
import com.neusoft.ruidaozaixian.util.baseinfo.Connectinfo;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.Response;

/**
 * A simple {@link Fragment} subclass.
 *
 */
public class PostInformationFragment extends Fragment {
    private int postId;
    private String postName;
    private String postDesc;
    private String postUrl;
    private int courseNums;
    private int courseHours;
    private int videoNums;
    private int testNums;
    private TextView postNameTv;
    private TextView courseNumsTv;
    private TextView courseHoursTv;
    private TextView videoNumsTv;
    private TextView testNumsTv;
    private TextView postDescTv;
    private ImageView postImg;
    private Button  button1;
    private ArrayList<PostInformation.PostStageResListBean> postStageResList;




    public PostInformationFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
       View view=inflater.inflate(R.layout.fragment_post_information, container, false);
        getDate();


        postNameTv= (TextView)view.findViewById(R.id.postNameTv);
        courseHoursTv= (TextView)view.findViewById(R.id.courseHoursTv);
        courseNumsTv= (TextView)view.findViewById(R.id.courseNumsTv);
        videoNumsTv= (TextView)view.findViewById(R.id.videoNumsTv);
        testNumsTv= (TextView)view.findViewById(R.id.testNumsTv);
        postDescTv= (TextView)view.findViewById(R.id.postDescTv);
        postImg= (ImageView)view.findViewById(R.id.postImg);
        button1= (Button)view.findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            /*    PostInformation postInformation = new PostInformation();
                postInformation.setPostStageResList(postStageResList);*/

           /*
                Bundle mBundle = new Bundle();
                mBundle.putSerializable(Connectinfo.POSTSKEY, postStageRes);
                intent.putExtras(mBundle);
                getActivity().startActivity(intent);
              */

                //使接口myListener得到content的值
                Intent intent = new Intent(getActivity().getApplicationContext(), PostActivity.class);
                getActivity().startActivity(intent);
                intent.putExtra("id",1);
                startActivity(intent);



            }
        });

        return  view;
    }
    public void getDate() {
        //创建一个Request
       FormBody formBody = new FormBody.Builder()
                .add("postid", PostCourseFragment.POST_ID)
                .build();
        final Request request = new Request.Builder()
                .addHeader("cookie", LoginActivity.s)
                .url(Connectinfo.obtainPositionClass)
                .post(formBody)
                .build();
        //new call
        Call call = LoginActivity.mOkHttpClient.newCall(request);
        //请求加入调度
        call.enqueue(new Callback() {
            //失败回掉
            @Override
            public void onFailure(Call call, IOException e) {

            }

            //成功回掉
            @Override
            public void onResponse(Call call, Response response) throws IOException {

                if (response.isSuccessful()) {

                    Message msg=Message.obtain();
                    msg.obj=response.body().string();
                    informationHandler.sendMessage(msg);

                }
            }

        });
    }

    private Handler informationHandler = new Handler() {
        public void handleMessage(Message msg) {
            String res = (String) msg.obj;
            Gson gson = new Gson();
            PostInformation postInformation=gson.fromJson(res, PostInformation.class);
            postId=postInformation.getPostMap().getPostId();
            postName=postInformation.getPostMap().getPostName();
            postDesc=postInformation.getPostMap().getPostDesc();
            postUrl=postInformation.getPostMap().getPostUrl();
            courseNums=postInformation.getPostMap().getCourseNums();
            courseHours=postInformation.getPostMap().getCourseHours();
            videoNums=postInformation.getPostMap().getVideoNums();
            testNums=postInformation.getPostMap().getTestNums();
            postStageResList=postInformation.getPostStageResList();





            postNameTv.setText(postName);
            courseHoursTv.setText("总课程："+courseHours+"门");
            courseNumsTv.setText("总课时："+courseHours+"小时");
            videoNumsTv.setText("视频数："+videoNums+"个");
            testNumsTv.setText("练习个数："+testNums+"个");
            postDescTv.setText(postDesc);
            Glide
                    .with(PostInformationFragment.this)
                    .load(postUrl)
                    .centerCrop()
                    .placeholder(R.mipmap.ic_launcher)//默认图
                    .crossFade()
                    .into(postImg);

        }
    };
}
